package org.compiere.grid;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Vector;

import org.compiere.minigrid.IMiniTable;
import org.compiere.model.GridTab;
import org.compiere.model.MInvoice;
import org.compiere.model.MLetterOfAttorney;
import org.compiere.model.MLetterOfAttorneyLine;
import org.compiere.model.MProduct;
import org.compiere.model.X_MM_Letter_Of_Attorney;
import org.compiere.model.X_MM_Letter_Of_Attorney_Line;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;

public class CreateFromOrder extends CreateFrom {

	public CreateFromOrder(GridTab mTab) {
		super(mTab);
		log.info(mTab.toString());
	}

	@Override
	public boolean dynInit() throws Exception {
		log.config("");
		setTitle(Msg.getElement(Env.getCtx(), "MM_Letter_Of_Attorney_Lines", false) + " .. " + Msg.translate(Env.getCtx(), "CreateFrom"));
		return true;
	}

	@Override
	public void info() {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean save(IMiniTable miniTable, String trxName) {
		int MM_Letter_Of_Attorney_ID = ((Integer)getGridTab().getValue("MM_Letter_Of_Attorney_ID")).intValue();
		MLetterOfAttorney letter_Of_Attorney = new MLetterOfAttorney(Env.getCtx(), MM_Letter_Of_Attorney_ID, trxName);
		log.config(letter_Of_Attorney.toString());

		if (p_order != null)
		{
			letter_Of_Attorney.setC_Order_ID(p_order.getC_Order_ID());	//	overwrite header values
			letter_Of_Attorney.saveEx();
		}
		
		p_order.getLines();
		
		//  Lines
		for (int i = 0; i < miniTable.getRowCount(); i++)
		{
			if (((Boolean)miniTable.getValueAt(i, 0)).booleanValue())
			{
				System.out.println("!!!Seleted line!!!");
				
				MProduct product = null;
				//  variable values
				BigDecimal QtyEntered = (BigDecimal)miniTable.getValueAt(i, 1);              //  1-Qty

				KeyNamePair pp = (KeyNamePair)miniTable.getValueAt(i, 2);   //  2-UOM
				int C_UOM_ID = pp.getKey();
				//
				pp = (KeyNamePair)miniTable.getValueAt(i, 3);               //  3-Product
				int M_Product_ID = 0;
				if (pp != null)
					M_Product_ID = pp.getKey();
				//
				int C_OrderLine_ID = 0;
				pp = (KeyNamePair)miniTable.getValueAt(i, 5);               //  5-OrderLine
				if (pp != null)
					C_OrderLine_ID = pp.getKey();
				int M_InOutLine_ID = 0;
				pp = (KeyNamePair)miniTable.getValueAt(i, 6);               //  6-Shipment
				if (pp != null)
					M_InOutLine_ID = pp.getKey();
				//
				int M_RMALine_ID = 0;
				pp = (KeyNamePair)miniTable.getValueAt(i, 7);               //  7-RMALine
				if (pp != null)
					M_RMALine_ID = pp.getKey();

				//	Precision of Qty UOM
				int precision = 2;
				if (M_Product_ID != 0)
				{
					product = MProduct.get(Env.getCtx(), M_Product_ID);
					precision = product.getUOMPrecision();
				}
				QtyEntered = QtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
				//
				log.fine("Line QtyEntered=" + QtyEntered
					+ ", Product_ID=" + M_Product_ID
					+ ", OrderLine_ID=" + C_OrderLine_ID + ", InOutLine_ID=" + M_InOutLine_ID);
				//Create new letter of attorney line
				MLetterOfAttorneyLine letterOfAttorneyLine = new MLetterOfAttorneyLine(letter_Of_Attorney);
				letterOfAttorneyLine.setC_Order_ID(p_order.getC_Order_ID());
				letterOfAttorneyLine.setM_Product_ID(M_Product_ID);
				letterOfAttorneyLine.setQuantity(QtyEntered.intValue());
				letterOfAttorneyLine.setC_UOM_ID(C_UOM_ID);
				letterOfAttorneyLine.saveEx();
			}
		}
		return true;
	}

	@Override
	protected ArrayList<KeyNamePair> loadOrderData(int C_BPartner_ID, boolean forInvoice, boolean sameWarehouseOnly) {
		
		return super.loadOrderData(C_BPartner_ID, forInvoice, sameWarehouseOnly);
	}

	@Override
	protected Vector<Vector<Object>> getOrderData(int C_Order_ID, boolean forInvoice) {

		return super.getOrderData(C_Order_ID, forInvoice);
	}
	protected Vector<String> getOISColumnNames(){
		//  Header Info
	    Vector<String> columnNames = new Vector<String>(7);
	    columnNames.add(Msg.getMsg(Env.getCtx(), "Select"));
	    columnNames.add(Msg.translate(Env.getCtx(), "Quantity"));
	    columnNames.add(Msg.translate(Env.getCtx(), "C_UOM_ID"));
	    columnNames.add(Msg.translate(Env.getCtx(), "M_Product_ID"));
	    columnNames.add(Msg.getElement(Env.getCtx(), "VendorProductNo", false));
	    columnNames.add(Msg.getElement(Env.getCtx(), "C_Order_ID", false));
	    columnNames.add(Msg.getElement(Env.getCtx(), "M_InOut_ID", false));
	    columnNames.add(Msg.getElement(Env.getCtx(), "M_RMA_ID", false));

	    return columnNames;		
	}
	protected void configureMiniTable (IMiniTable miniTable)
	{
		miniTable.setColumnClass(0, Boolean.class, false);      //  0-Selection
		miniTable.setColumnClass(1, BigDecimal.class, true);        //  1-Qty
		miniTable.setColumnClass(2, String.class, true);        //  2-UOM
		miniTable.setColumnClass(3, String.class, true);        //  3-Product
		miniTable.setColumnClass(4, String.class, true);        //  4-VendorProductNo
		miniTable.setColumnClass(5, String.class, true);        //  5-Order
		miniTable.setColumnClass(6, String.class, true);        //  6-Ship
		miniTable.setColumnClass(7, String.class, true);        //  7-Invoice
		//  Table UI
		miniTable.autoSize();
	}
	

}
